<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_block_ContextMenuAction extends m_backoffice_extends_Action
{
	public function execute()
	{
		// Set to the routing class (see webapp/config/routes.xml to set routes)
		$routing = f_core_Routing::getInstance()->setRequestURL($this->getRequestParameter('href'));
		$docId = $this->getRequestParameter("docId");
		$docLang = $this->getRequestParameter("docLang");

		$module = $this->getRequestParameter("module");
		
		if(!is_null($this->getRequestParameter("configModule")) && $this->getRequestParameter("configModule") != "")
		{
			$module = $this->getRequestParameter("configModule");
		}

		if($module == "backoffice") return "";

		$contextMenuService = m_backoffice_services_ContextMenuService::getInstance();

		// If no doc id, get current module rootNode
		if(is_null($docId))
		{
			if(is_null($module)) $module = "website";
			$document = m_backoffice_services_RootNodeService::getInstance()->getModuleRootNode($module);
		}
		else
		{
			$document = $this->getDocumentProvider()->getByUniqueId($docId, $docLang);
		}

        if($document->getModel() == 'modules_backoffice/rootNode')
        {
            $document->setModule($module);
        }

		$items = $contextMenuService->getContextMenuItems($document);

		if($document->isLocalized() && !$document->isTranslated() || !isset($items) || count($items) == 0)
		{
			return "";
		}

		$this->addToModel("rightClickItems", $contextMenuService->getRightClickItems($document));
		$this->addToModel("items", $items);
		
		$this->setView("success");
	}

    public function errorHandler()
    {
        return null;
    }
}